#pragma once

#include <opencv2/opencv.hpp>

using namespace cv;

/// <summary>
/// 对图像像素依次处理  获得图像反色操作
/// </summary>
/// <param name="image"></param>
void  PixelVisit_Demo(Mat& image)
{
	int w = image.cols;
	int h = image.rows;
	int dims = image.channels();

	for (int row = 0; row < h; row++)
	{
		for (int col = 0; col < w; col++)
		{
			//灰度图像
			if (dims == 1)
			{
				int pv = image.at<uchar>(row, col);
				image.at<uchar>(row, col) = 255 - pv;
			}

			//彩色图像
			if (dims == 3)
			{
				Vec3b bgr = image.at<Vec3b>(row, col);
				image.at<Vec3b>(row, col)[0] = 255 - bgr[0];
				image.at<Vec3b>(row, col)[1] = 255 - bgr[1];
				image.at<Vec3b>(row, col)[2] = 255 - bgr[2];
			}
		}

	}

	imshow("读写演示", image);

}

/// <summary>
/// 对图像像素依次处理 指针处理  获得图像反色操作  
/// </summary>
/// <param name="image"></param>
void  PixelVisitPtr_Demo(Mat& image)
{
	int w = image.cols;
	int h = image.rows;
	int dims = image.channels();

	for (int row = 0; row < h; row++)
	{
		//得到当前行的指针地址
		uchar* current_row = image.ptr<uchar>(row);
		for (int col = 0; col < w; col++)
		{
			//灰度图像
			if (dims == 1)
			{
				int pv = *current_row;
				*current_row++ = 255 - pv;
			}

			//彩色图像
			if (dims == 3)
			{
				int pv = *current_row;
				/*	*current_row++ = 255 - pv;
					*current_row++ = 255 - pv;
					*current_row++ = 255 - pv;*/

				*current_row++ = 255 - *current_row;
				*current_row++ = 255 - *current_row;
				*current_row++ = 255 - *current_row;
			}
		}

	}

	imshow("读写演示", image);

}
